﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Reflection;
using Cqrs.Infrastructure.Aggregate;
using Cqrs.StateMachineDesigner.Database;
using Cqrs.Infrastructure;
using System.Xml.Linq;

namespace Cqrs.StateMachineDesigner.CustomCode
{
    /// <summary>
    /// Interaction logic for LoadComponent.xaml
    /// </summary>
    public partial class LoadComponent : Window
    {
        private ComponentModel _componentModel;
        private Assembly _assembly;
        private Type _assemblyType;

        public LoadComponent()
        {
            InitializeComponent();
        }

        public bool EnableOpen
        {
            get { return txtAssemblyLocation.Text.Length > 0 && txtSmfFile.Text.Length > 0 && txtDatabase.Text.Length > 0; }
        }

        private void btnLoadAssembly_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFile = new OpenFileDialog();
                openFile.Filter = "Assembly Files (*.dll)|*.dll|(*.exe)|*.exe";

                if (openFile.ShowDialog() == true)
                {
                    string fileName = openFile.FileName;
                    _assembly = Assembly.LoadFrom(openFile.FileName);
                    SelectDomainType selectForm = new SelectDomainType();
                    _assemblyType = selectForm.select(_assembly);

                    DisplayComponentModel();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DisplayComponentModel()
        {
            if (_assemblyType == null) return;

            txtAssemblyLocation.Text = _assembly.Location;
            txtAssemblyName.Text = _assembly.FullName;
            txtDatabase.Text = System.IO.Path.Combine(
                    System.IO.Path.GetDirectoryName(_assembly.Location),
                    "Db");

            AggregateRootWithState aggregateRoot = AggregateRootWithState.Load(_assemblyType, txtSmfFile.Text);
            txtStateMachineXml.Text = aggregateRoot.GetSmfFileParser().GetStateMachineXml().ToString();
            txtSmfFile.Text = aggregateRoot.GetSmfFileLocation();

            _componentModel = new ComponentModel
            {
                AssemblyLocation = _assembly.Location,
                AssemblyType = _assemblyType.AssemblyQualifiedName,
                DomainName = aggregateRoot.GetSmfFileParser().GetDomainName(),
                DomainProperties = DomainProperty.ConvertXElementsToDomainProperties(
                        aggregateRoot.GetSmfFileParser().GetDomainProperties()),
                DesignerXml = aggregateRoot.GetSmfFileParser().GetStateMachineDesigner(),
                Scxml = aggregateRoot.GetSmfFileParser().GetStateMachineXml(),
                DbLocation = txtDatabase.Text,
                SmfFile = txtSmfFile.Text
            };
            btnOpen.IsEnabled = true;
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public ComponentModel LoadComponentModel(ComponentModel componentModel)
        {
            txtSmfFile.Text = componentModel.SmfFile;
            txtAssemblyLocation.Text = componentModel.AssemblyLocation;
            txtAssemblyName.Text = componentModel.AssemblyType;
            txtDatabase.Text = componentModel.DbLocation;
            if (componentModel.DesignerXml != null)
                txtStateMachineXml.Text = componentModel.DesignerXml.ToString();
            btnOpen.IsEnabled = false;
            this.ShowDialog();
            return _componentModel;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _componentModel = null;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                _componentModel = null;
            }
            finally
            {
            }
        }

        private void btnSelectDatabase_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (System.Windows.Forms.FolderBrowserDialog folderBrowser =
                    new System.Windows.Forms.FolderBrowserDialog())
                {
                    if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        txtDatabase.Text = folderBrowser.SelectedPath;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSelectSmfFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFile = new OpenFileDialog();
                openFile.Filter = "Assembly Files (*.smf)|*.smf";
                if (openFile.ShowDialog() == true)
                {
                    txtSmfFile.Text = openFile.FileName;
                    DisplayComponentModel();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
